package img

import (
	"common/models"
	"common/validate"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"
	"os"
)

// DeleteImg 删除图片
// @Summary 删除图片
// @Description 删除图片
// @Tags 图片
// @Accept application/json
// @Produce application/json
// @Param object body docModel.DocDeleteImgReq false "删除参数"
// @Param Authorization header string true "Bearer 用户令牌"
// @Security ApiKeyAuth
// @Success 200 {object} docModel.DocResp
// @Router /img/delete [post]
func (h IHandler) DeleteImg(c *gin.Context) {
	resp := new(models.RespData)
	req := new(models.DeleteImgReq)
	if err := c.ShouldBindJSON(req); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			zap.L().Error("Img:SearchImgByAuthorId with invalid fail", zap.Error(err))
			resp.SendServerBusy(c)
			return
		} else {
			zap.L().Error("Img:SearchImgByAuthorId with invalid fail", zap.Error(errs))
			resp.SendCodeWithMsg(c, models.CodeInvalidParam, validate.RemoveTopStruct(errs.Translate(validate.Trans)))
			return
		}
	}
	src, err := h.M.ImgInfoDelByUid(req.ImgId)
	if err != nil {
		zap.L().Error("Img:h.M.ImgInfoDelByUid error", zap.Error(err))
		return
	}
	//移除文件
	err = os.Remove(src)
	if err != nil {
		zap.L().Error("Img:os.ReadFile", zap.Error(err))
	}
	resp.SendSuccess(c)
}
